package
{
	import com.hurlant.crypto.symmetric.CBCMode;
	import com.hurlant.crypto.symmetric.DESKey;
	import com.hurlant.crypto.symmetric.ECBMode;
	import com.hurlant.util.Base64;
	
	import flash.display.Sprite;
	import flash.utils.ByteArray;
	
	public class Main extends Sprite
	{
		private const KEY_STR:String="phhuicom";  
		public function Main()
		{
			
			var b_ba:ByteArray=encrypt(KEY_STR,"abcdefg");  
			//字符串形式的密文  
			var b_ciphertext:String=Base64.encodeByteArray(b_ba);  
			trace("密文:",b_ciphertext);  
			
			var b_ciphertext16:String=byteArrayTo16(b_ba);  
			trace("十六进制形式密文:",b_ciphertext16);  
			
			//解密后的原文  
			var b_originalText :String=decrypt(KEY_STR,Base64.decodeToByteArray(b_ciphertext));  
			trace("解密得到：",b_originalText);  
			
			//解密16进制密文后的原文  
			var b_originalText16 :String=decrypt(KEY_STR,from16ToByteArray(b_ciphertext16));  
			trace("解密十六进制密文得到：",b_originalText16);  
			
		}
		
		/** 
		 * 加密 
		 * @param key 加解密用的key 
		 * @param data 被加密的字符串 
		 * @return 加密后生成ByteArray数据 
		 */       
		private function encrypt(key:String,data:String):ByteArray{  
			trace("执行加密方法，key:",key,"，被加密的字符串：",data);  
			
			//实验化key的Bytearray对象，给DESKey使用  
			var b_keyByteArray:ByteArray=new ByteArray();   
			b_keyByteArray.writeUTFBytes(key);  
			
			//实例化DESKey  
			var b_desKey:DESKey=new DESKey(b_keyByteArray);  
			
			//不只是有ecb还有cbc,cfb等，有兴趣可以自己尝试  
			var b_ecb:ECBMode=new ECBMode(b_desKey);  
			//把需要加密字符串转换为ByteArray  
			var b_byteArray:ByteArray=convertStringToByteArray(data);  
			
			//执行加密  
			b_ecb.encrypt(b_byteArray);  
			
			return b_byteArray;  
		}  
		/** 
		 * 解密 
		 * @param key 解密用的key 
		 * @param data 需要解密的数据 
		 * @return 解密后的数据 
		 */       
		private function decrypt(key:String,data:ByteArray):String{  
			trace("\n执行解密方法，key:",key,"，需要解密的字符串：",data);  
			
			//实验化key的Bytearray对象，给DESKey使用  
			var b_keyByteArray:ByteArray=new ByteArray();   
			b_keyByteArray.writeUTFBytes(key);  
			
			//实例化DESKey  
			var b_desKey:DESKey=new DESKey(b_keyByteArray);  
			
			//  
			var b_ecb:ECBMode=new ECBMode(b_desKey);  
			
			var b_byteArray:ByteArray=new ByteArray();  
			b_byteArray.writeBytes(data);  
			
			//执行解密  
			b_ecb.decrypt(b_byteArray);  
			
			return convertByteArrayToString(b_byteArray);  
		}  
		
		/** 
		 * String转ByteArray  
		 * @param str 
		 * @return  
		 */       
		public function convertStringToByteArray(str:String):ByteArray   
		{   
			var bytes:ByteArray;   
			if (str)   
			{   
				bytes=new ByteArray();   
				bytes.writeUTFBytes(str);   
			}   
			return bytes;   
		}   
		/** 
		 * ByteArray转String 
		 * @param bytes 
		 * @return  
		 */       
		public function convertByteArrayToString(bytes:ByteArray):String   
		{   
			var str:String;   
			if (bytes)   
			{   
				bytes.position=0;   
				str=bytes.readUTFBytes(bytes.length);   
			}   
			return str;   
		}  
		/** 
		 * 把ByteArray转换为16进制的形式的字符串 
		 * @param ba 
		 * @param name 
		 * @return  
		 */       
		private function byteArrayTo16(ba:ByteArray):String{  
			ba.position=0;  
			var b_str:String="";  
			while (ba.bytesAvailable > 0) {  
				var b_s:String=ba.readUnsignedByte().toString(16);  
				//              trace("b_s:",b_s);  
				if(b_s.length<2) b_s="0"+b_s;  
				b_str+=b_s;  
			}  
			return b_str;  
		}  
		private function from16ToByteArray(str:String):ByteArray{  
			var b_ba:ByteArray=new ByteArray();  
			var b_arr:Array=str.split("");  
			var b_length:int=b_arr.length;  
			for(var i:int=0;i<b_length-1;i+=2){  
				var b_char1:String=b_arr[i];  
				var b_char2:String=b_arr[i+1];  
				var b_byte:int=parseInt(b_char1+b_char2,16);  
				//              trace("b_byte:",b_byte);  
				b_ba.writeByte(b_byte);  
			}  
			return b_ba;  
		}  
	}
}